use iced::{Element, Length, Padding};
use iced::widget::{container, Container};
use crate::theme::CdbTheme;
use super::{spacing, radius, shadow};

/// Card variants
#[derive(Debug, Clone, Copy)]
pub enum CardVariant {
    Default,
    Elevated,
    Outlined,
    Filled,
}

/// Create a modern card component
pub fn card<'a, Message>(
    content: Element<'a, Message>,
    variant: CardVariant,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    container(content)
        .style(move |_theme| {
            let colors = &theme.colors;
            
            match variant {
                CardVariant::Default => container::Style {
                    background: Some(colors.card.into()),
                    border: iced::Border {
                        color: colors.border,
                        width: 1.0,
                        radius: radius::LG.into(),
                    },
                    shadow: iced::Shadow {
                        color: shadow::SM,
                        offset: iced::Vector::new(0.0, 1.0),
                        blur_radius: 3.0,
                    },
                    ..Default::default()
                },
                CardVariant::Elevated => container::Style {
                    background: Some(colors.card.into()),
                    border: iced::Border {
                        color: colors.border,
                        width: 1.0,
                        radius: radius::LG.into(),
                    },
                    shadow: iced::Shadow {
                        color: shadow::LG,
                        offset: iced::Vector::new(0.0, 4.0),
                        blur_radius: 6.0,
                    },
                    ..Default::default()
                },
                CardVariant::Outlined => container::Style {
                    background: Some(colors.card.into()),
                    border: iced::Border {
                        color: colors.border,
                        width: 2.0,
                        radius: radius::LG.into(),
                    },
                    shadow: iced::Shadow::default(),
                    ..Default::default()
                },
                CardVariant::Filled => container::Style {
                    background: Some(colors.muted.into()),
                    border: iced::Border {
                        color: iced::Color::TRANSPARENT,
                        width: 0.0,
                        radius: radius::LG.into(),
                    },
                    shadow: iced::Shadow::default(),
                    ..Default::default()
                },
            }
        })
        .padding(spacing::LG)
}

/// Create a card header
pub fn card_header<'a, Message>(
    content: Element<'a, Message>,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    container(content)
        .style(move |_theme| {
            container::Style {
                background: None,
                border: iced::Border {
                    color: theme.colors.border,
                    width: 0.0,
                    radius: 0.0.into(),
                },
                shadow: iced::Shadow::default(),
                ..Default::default()
            }
        })
        .padding(spacing::MD)
}

/// Create a card content area
pub fn card_content<'a, Message>(
    content: Element<'a, Message>,
    _theme: &'a CdbTheme,
) -> Container<'a, Message> {
    container(content)
        .style(move |_theme| {
            container::Style {
                background: None,
                border: iced::Border::default(),
                shadow: iced::Shadow::default(),
                ..Default::default()
            }
        })
        .padding(Padding::from([spacing::SM, 0]))
}

/// Create a card footer
pub fn card_footer<'a, Message>(
    content: Element<'a, Message>,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    container(content)
        .style(move |_theme| {
            container::Style {
                background: None,
                border: iced::Border {
                    color: theme.colors.border,
                    width: 0.0,
                    radius: 0.0.into(),
                },
                shadow: iced::Shadow::default(),
                ..Default::default()
            }
        })
        .padding(spacing::MD)
}

/// Create a dashboard card with icon and stats
pub fn dashboard_card<'a, Message: 'a>(
    title: &'a str,
    value: &'a str,
    description: &'a str,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    use iced::widget::{column, text, row};
    
    let title_text = text(title)
        .size(super::typography::SM)
        .color(theme.colors.muted_foreground);
    
    let value_text = text(value)
        .size(super::typography::XXL)
        .color(theme.colors.foreground);
    
    let description_text = text(description)
        .size(super::typography::XS)
        .color(theme.colors.muted_foreground);
    
    let content = column![
        title_text,
        value_text,
        description_text
    ]
    .spacing(spacing::SM);
    
    card(content.into(), CardVariant::Default, theme)
}

/// Create a connection status card
pub fn connection_status_card<'a, Message: 'a>(
    database_name: &'a str,
    status: &'a str,
    is_connected: bool,
    theme: &'a CdbTheme,
) -> Container<'a, Message> {
    use iced::widget::{column, text, row};
    
    let status_color = if is_connected {
        theme.colors.chart_2 // Green
    } else {
        theme.colors.destructive // Red
    };
    
    let db_name = text(database_name)
        .size(super::typography::LG)
        .color(theme.colors.foreground);
    
    let status_text = text(status)
        .size(super::typography::SM)
        .color(status_color);
    
    let content = column![
        db_name,
        status_text
    ]
    .spacing(spacing::SM);
    
    card(content.into(), CardVariant::Default, theme)
}
